<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
    <el-col>
      <h3 v-if="isTools">{{ data.name }}</h3>
      <span v-else>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="isTools" @click.native="addDepartments">添加部门</el-dropdown-item>
              <el-dropdown-item v-else @click.native="addDepartments">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isTools" @click.native="editDepartments">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isTools" @click.native="delDepartments(data.id)">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsById } from '@/api/departments'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    isTools: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

    }
  },
  methods: {
    // 删除部门
    async delDepartments(id) {
      try {
        // 提示是否删除
        await this.$confirm('是否确定删除该部门？')
        // 发请求
        await delDepartmentsById(id)
        // 弹出提示
        this.$message.success('删除成功')
        // 更新页面
        this.$emit('delDepartments')
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    addDepartments() {
      this.$emit('addDepartments', this.data)
    },
    // 编辑部门
    editDepartments() {
      this.$emit('editDepartments', this.data)
    }
  }
}
</script>

<style>
.el-tree-node__content{
  height: 40px;
}
</style>
